package com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.sbsssbtz.determinations;

import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.determination.*;
import com.inspur.edp.bef.spi.action.determination.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.spi.action.determination.AbstractDetermination;
import com.inspur.edp.bef.api.action.determination.IDeterminationContext;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.bef.api.action.determination.IBeforeRetrieveDtmContext;
import com.inspur.edp.svc.document.storage.api.DocStorageService;
import com.inspur.edp.svc.document.storage.entity.GspDocContent;
import com.inspur.edp.svc.document.storage.entity.GspDocMetadata;
import com.inspur.edp.svc.document.storage.entity.GspDocOperatingModes;
import com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.sbsssbtz.common.QrMetaData;
import com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.sbsssbtz.common.SbssSbtzQrImageUtil;
import com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.util.QRUtil;
import io.iec.edp.caf.commons.qrcode.api.GspQrCodeGenerateService;
import io.iec.edp.caf.commons.qrcode.core.enums.CorrectionLevel;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.rpc.client.RpcClassHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;

public class SbssSbtzDtmBeforeSave1Determination extends AbstractB4SaveDetermination {
    public SbssSbtzDtmBeforeSave1Determination(IDeterminationContext context, IChangeDetail change) {
        super(context, change);
    }

    @Override
    public void execute() {
        IEntityData currData = getContext().getCurrentData();
        if (currData == null) {
            return;
        }
        if (currData.getValue("sbbh") != null && !StringUtils.isBlank(currData.getValue("sbbh").toString())) {
            SbssSbtzQrImageUtil qrImageUtil = new SbssSbtzQrImageUtil();
            QrMetaData qrMetaData = qrImageUtil.updQRCode(currData.getValue("sbbh").toString());

            GspDocMetadata metadata = qrMetaData.getMetadata();
            String attId = qrMetaData.getAttachmentId();

            HashMap<String, Object> map = new HashMap<>();
            map.put("AttachmentId", attId);
            map.put("FileName", metadata.getFileName() + ".png");
            map.put("FileSize", new BigDecimal(metadata.getDocSize()));
            map.put("FileCreateTime", new Date());
            getData().setMultiUdtValue("qrcode", map);
        }
    }

    private IEntityData getData() {
        return (IEntityData) super.getContext().getCurrentData();
    }
}